package com.android.killtencent.activity;

import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.EditText;

import com.android.killtencent.R;
import com.android.killtencent.task.RegisterTask;
import com.android.pc.ioc.inject.InjectInit;
import com.android.pc.ioc.inject.InjectLayer;
import com.android.pc.ioc.inject.InjectListener;
import com.android.pc.ioc.inject.InjectMethod;
import com.android.pc.ioc.inject.InjectView;
import com.android.pc.ioc.verification.Rule;
import com.android.pc.ioc.verification.Validator;
import com.android.pc.ioc.verification.annotation.ConfirmPassword;
import com.android.pc.ioc.verification.annotation.Password;
import com.android.pc.ioc.verification.annotation.Regex;
import com.android.pc.ioc.view.listener.OnClick;

/**
 * 作者:	Rose
 * 业务名:    注册界面
 * 功能说明: 注册逻辑处理
 * 编写日期:    2015-02-14
 */
@InjectLayer(R.layout.activity_register_layout)
public class RegisterActivity extends Activity implements Validator.ValidationListener {

    @InjectView(R.id.user_register_edit)
    @Regex(message = "输入内容错误", trim = true, pattern = "[a-zA-Z0-9_]{6,15}", order = 6)
    private EditText userEdit;

    @InjectView(R.id.passeword_register1_edit)
    @Password(order = 1, message = "密码长度错误", minLength = 6, maxLength = 18)
    private EditText userPassedEdit;

    @ConfirmPassword(message = "两次密码输入不一致", order = 2)
    @InjectView(R.id.passeword_register2_edit)
    private EditText userPassedSureEdit;

    private Validator validator;

    @InjectInit
    private void init() {
        validator = new Validator(this);
        validator.setValidationListener(this);
    }

    @InjectMethod(@InjectListener(ids = {R.id.register_ok_btn, R.id.to_login_tv}, listeners = {OnClick.class}))
    public void setOncliect(View view) {
        switch (view.getId()) {
            case R.id.register_ok_btn:
                validator.validate();
                break;
            case R.id.to_login_tv:
                startActivity(new Intent(RegisterActivity.this, LoginActivity.class));
                this.finish();
                break;
        }
    }


    @Override
    public void onValidationSucceeded() {
        String name = userEdit.getText().toString().trim();
        String psw = userPassedEdit.getText().toString().trim();
        new RegisterTask(name,psw,this).execute();
    }

    @Override
    public void onValidationFailed(View view, Rule<?> rule) {

    }
}
